<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>权限管理</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/box.css" />
    <script type="text/javascript" src="./script/public.js"></script>
    <style>
    	.marg{
    		margin:3px 15px;
    		font-size:18px;
    	}
    	
    </style>
    <script type="text/javascript">
		
		apiready = function(){
	    	
	    }
	    
	    function hasPermission(one_per){
	    	var perms = new Array();
	    	if(one_per){
	    		perms.push(one_per);
	    	}else{
	    		var prs = document.getElementsByName("p_list");
	            for(var i = 0; i < prs.length; i++){
		            if(prs[i].checked){
		            	perms.push(prs[i].value);
		            }
	         	}
	    	}
         	var rets = api.hasPermission({
         		list:perms
         	});
         	if(!one_per){
         		apialert('判断结果：' + JSON.stringify(rets));
         		return;
         	}
         	return rets;
	    }
	    
	    function reqPermission(one_per, callback){
	    	var perms = new Array();
	    	if(one_per){
	    		perms.push(one_per);
	    	}else{
		    	var prs = document.getElementsByName("p_list_r");
	            for(var i = 0; i < prs.length; i++){
		            if(prs[i].checked){
		            	perms.push(prs[i].value);
		            }
	         	}
	         }
         	api.requestPermission({
         		list: perms,
         		code: 100001
         	}, function(ret, err){
         		if(callback){
         			callback(ret);
         			return;
         		}
         		var str = '请求结果：\n';
         		str += '请求码: ' + ret.code + '\n';
         		str += "是否勾选\"不再询问\"按钮: " + (ret.never ? '是' : '否') + '\n';
         		str += '请求结果: \n';
         		var list = ret.list;
         		for(var i in list){
         			str += list[i].name + '=' + list[i].granted + '\n';
         		}
         		apialert(str);
         		console.log(JSON.stringify(ret));
         	});
	    }
	    
	    function opWithPermission(perm){
	    	if(!confirmPer(perm)){
	    		return;
	    	}
	    	if('calendar' == perm){
	    		//操作日历
	    	}else if('camera' == perm){
	    		api.getPicture({
				    sourceType: 'camera',
				    mediaValue: 'pic',
				    destinationType: 'url',
				}, function(ret, err) {
				    if (ret) {
				        apialert(JSON.stringify(ret));
				    } else {
				        apialert(JSON.stringify(err));
				    }
				});
	    	}else if('contacts' == perm){
	    		api.openContacts({
	    			test: true
	    		}, function(ret, err) {
				    if (ret && ret.status) {
				        apialert(JSON.stringify(ret));
				    } else {
				        apialert(JSON.stringify(err));
				    }
				});
	    	}else if('location' == perm){
	    		api.getLocation(function(ret, err) {
				    if (ret && ret.status) {
				        apialert(JSON.stringify(ret));
				    } else {
				        apialert(JSON.stringify(err));
				    }
				});
	    	}else if('microphone' == perm){
	    		api.startRecord({
				    path: 'fs://perm-test.amr'
				});
	    	}else if('phone' == perm){
	    		api.call({
				    type: 'tel',
				    number: '10086'
				});
	    	}else if('sensor' == perm){
	    		//操作身体传感器
	    	}else if('sms' == perm){
	    		api.sms({
				    numbers: ['10086'],
				    text: '余额',
				    silent: true
				});
	    	}else if('storage' == perm){
	    		api.readFile({
	    			path:'fs://test.txt'
	    		}, function(ret, err) {
				    if (ret.status) {
				        console.log('readFile: ' + ret.data);
				    } else {
				        apialert(err.msg + ": \n" + api.fsDir);
				    }
				});
	    	}
	    }
	    
	    function confirmPer(perm){
	    	var has = hasPermission(perm);
	    	if(!has || !has[0] || !has[0].granted){
		    	api.confirm({
				    title: '提醒',
				    msg: '没有获得 ' + perm + " 权限\n是否前往设置？",
				    buttons: ['去设置', '取消']
				}, function(ret, err) {
				    if(1 == ret.buttonIndex){
				    	reqPermission(perm);
				    }
				});
	    		return false;
	    	}
	    	return true;
	    }
	    
	</script>
</head>
<body>
    <div >
        <div id="wrap">
        	<div id='header'>
		        <div class="back" tapmode="back-active" onclick="api.closeWin()" >返回</div>
		        <h1>权限管理测试</h1>
		        <div class="adpt" ></div>
			</div> 
        	<div class='itemtitle'>一、判断权限</div>
			<div class='marg'>请选择一个或者多个权限进行判断:</div>
        	<div class='marg'>日历&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="calendar" /></div>
			<div class='marg'>相机&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="camera" /></div>
			<div class='marg'>通讯录&emsp;&emsp;<input type="checkbox" name="p_list" value="contacts" /></div>
			<div class='marg'>位置信息&emsp;<input type="checkbox" name="p_list" value="location" /></div>
			<div class='marg'>麦克风&emsp;&emsp;<input type="checkbox" name="p_list" value="microphone" /></div>
			<div class='marg'>电话&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="phone" /></div>
			<div class='marg'>身体传感器<input type="checkbox" name="p_list" value="sensor" /></div>
			<div class='marg'>短信&emsp;&emsp;&emsp;<input type="checkbox" name="p_list" value="sms" /></div>
			<div class='marg'>存储空间&emsp;<input type="checkbox" name="p_list" value="storage" /></div>
        	<div class="clickbtn" tapmode="active" onclick="hasPermission()" >点击开始判断</div>
        	<div class='itemtitle'>二、请求权限</div>
    		<div class='marg'>请选择一个或者多个权限进行请求:</div>
        	<div class='marg'>日历&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="calendar" /></div>
			<div class='marg'>相机&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="camera" /></div>
			<div class='marg'>通讯录&emsp;&emsp;<input type="checkbox" name="p_list_r" value="contacts" /></div>
			<div class='marg'>位置信息&emsp;<input type="checkbox" name="p_list_r" value="location" /></div>
			<div class='marg'>麦克风&emsp;&emsp;<input type="checkbox" name="p_list_r" value="microphone" /></div>
			<div class='marg'>电话&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="phone" /></div>
			<div class='marg'>身体传感器<input type="checkbox" name="p_list_r" value="sensor" /></div>
			<div class='marg'>短信&emsp;&emsp;&emsp;<input type="checkbox" name="p_list_r" value="sms" /></div>
			<div class='marg'>存储空间&emsp;<input type="checkbox" name="p_list_r" value="storage" /></div>
        	<div class="clickbtn" tapmode="active" onclick="reqPermission()" >点击开始请求</div>
        	<div class='itemtitle'>三、需要权限的API操作</div>
        	<div class='marg'>1、日历</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('calendar')" >点击操作日历</div>
			<div class='marg'>2、相机</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('camera')" >点击操作照相机</div>
			<div class='marg'>3、通讯录</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('contacts')" >点击操作通讯录</div>
			<div class='marg'>4、位置信息</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('location')" >点击操作位置信息</div>
			<div class='marg'>5、麦克风</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('microphone')" >点击操作麦克风</div>
			<div class='marg'>6、电话</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('phone')" >点击操作电话</div>
			<div class='marg'>7、身体传感器</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('sensor')" >点击操作身体传感器</div>
			<div class='marg'>8、短信</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('sms')" >点击操作短信</div>
			<div class='marg'>9、存储空间</div>
			<div class="clickbtn" tapmode="active" onclick="opWithPermission('storage')" >点击操作存储空间</div>
            <br>
        </div>   
    </div>
</body>
</html>